
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>初学者可能不知道的vue技巧 - 默默默默燃</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="一枚前端搬砖队队员的记录册,技巧/坑点setTimeout/ setInterval
场景一 ：this指向改变无法用this访问vue实例

123456mounted()&amp;#123;  setTimeout( functi,"> 
    <meta name="author" content="张白告丶"> 
    <link rel="alternative" href="atom.xml" title="默默默默燃" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">默默默默燃</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://zhanghao-web.github.io">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">初学者可能不知道的vue技巧</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/Vue"><b>「
                    </b>VUE<b> 」</b></a>
                
                October 21, 2018
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2018/10/21/Vue/%E5%88%9D%E5%AD%A6%E8%80%85%E5%8F%AF%E8%83%BD%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84vue%E6%8A%80%E5%B7%A7/" title="初学者可能不知道的vue技巧" class="">初学者可能不知道的vue技巧</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>Words count</i>
                    20k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>Reading time</i>
                    18 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>Read count</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/Vue/" rel="tag">Vue</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <h1 id="技巧-坑点"><a href="#技巧-坑点" class="headerlink" title="技巧/坑点"></a>技巧/坑点</h1><h2 id="setTimeout-setInterval"><a href="#setTimeout-setInterval" class="headerlink" title="setTimeout/ setInterval"></a>setTimeout/ setInterval</h2><ul>
<li>场景一 ：this指向改变无法用this访问vue实例</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123;</span><br><span class="line">  setTimeout( <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">//setInterval同理</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="keyword">this</span>); <span class="comment">//此时this指向Window对象</span></span><br><span class="line">  &#125;，<span class="number">1000</span>) ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>解决方法 :使用箭头函数或者缓存this</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">//箭头函数访问this实例因为箭头函数本身没有绑定this</span></span><br><span class="line">  setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>. log(<span class="keyword">this</span>);&#125;,  <span class="number">500</span>) ;</span><br><span class="line">    <span class="comment">//使用变量访问this实例let self=this;</span></span><br><span class="line">  &#125;,<span class="number">1000</span>);</span><br><span class="line">  setTimeout (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>. log(self);<span class="comment">//使用self变量访问this实例</span></span><br><span class="line">  &#125;, <span class="number">1000</span>) ;</span><br><span class="line">​</span><br></pre></td></tr></table></figure>
<h3 id="setInterval路由跳转继续运行并没有销毁"><a href="#setInterval路由跳转继续运行并没有销毁" class="headerlink" title="setInterval路由跳转继续运行并没有销毁"></a>setInterval路由跳转继续运行并没有销毁</h3><blockquote>
<p>场景一 :比如一些弹幕，走马灯文字，这类需要定时调用的，路由跳转之后，因为组件已经销毁了，但是setlnterval还没有销毁，还在继续后台调用，控制台会不断报错，如果运算量大的话，无法及时清除，会导致严重的页面卡顿。</p>
</blockquote>
<ul>
<li>解决方法 :在组件生命周期beforeDestroy停止setInterval</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">created() &#123;</span><br><span class="line">     <span class="keyword">this</span>.intervalid = setInterval(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">this</span>.layerError = <span class="string">""</span>;</span><br><span class="line">        <span class="keyword">this</span>.Timer = <span class="literal">null</span>;</span><br><span class="line">    &#125;, <span class="number">100000</span>);</span><br><span class="line"> &#125;</span><br><span class="line">beforeDestroy( )&#123;</span><br><span class="line">  <span class="comment">//我通常是把setInterval( )定时器赋值给this实例，然后就可以像下面这么暂停。</span></span><br><span class="line">  clearInterval(<span class="keyword">this</span>.intervalid);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="Vue路由拦截浏览器后退实现草稿保存类似需求"><a href="#Vue路由拦截浏览器后退实现草稿保存类似需求" class="headerlink" title="Vue路由拦截浏览器后退实现草稿保存类似需求"></a>Vue路由拦截浏览器后退实现草稿保存类似需求</h2><ul>
<li><p>场景一 :为了防止用户突然离开，没有保存已输入的信息</p>
</li>
<li><p>解决方法 :</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">​</span><br><span class="line">  <span class="comment">//在路由组件中:mounted()&#123;&#125;，</span></span><br><span class="line">  beforeRouteLeave (to, <span class="keyword">from</span>, next) &#123;</span><br><span class="line">      <span class="keyword">if</span>(用户已经输入信息)&#123;</span><br><span class="line">        <span class="comment">//出现弹窗提醒保存草稿，或者自动后台为其保存</span></span><br><span class="line">      &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        next(<span class="literal">true</span>);<span class="comment">//用户离开</span></span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">​</span><br></pre></td></tr></table></figure>
<h2 id="自定义组件添加click等事件不生效"><a href="#自定义组件添加click等事件不生效" class="headerlink" title="自定义组件添加click等事件不生效"></a>自定义组件添加click等事件不生效</h2><ul>
<li>场景一 :一些自定义组件，需要额外添加一些事件来实现一些特定需求</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-progress</span> <span class="attr">type</span>=<span class="string">"circle"</span> <span class="attr">:percentage</span>=<span class="string">“0</span>" @<span class="attr">click</span>=<span class="string">“stopProgress”</span>&gt;</span><span class="tag">&lt;/<span class="name">elprogress</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">     methods:&#123;</span><br><span class="line">          stopProgress() &#123; </span><br><span class="line"><span class="javascript">          <span class="built_in">console</span>.log(<span class="string">'停止'</span>)</span></span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<ul>
<li>解决方法:使用.native修饰符</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-progress</span> <span class="attr">type</span>=<span class="string">"circle"</span> <span class="attr">:percentage</span>=<span class="string">“0</span>" @<span class="attr">click.native</span>=<span class="string">“stopProgress”</span>&gt;</span><span class="tag">&lt;/<span class="name">el-progress</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">      methods:&#123;</span><br><span class="line">          stopProgress() &#123; </span><br><span class="line"><span class="javascript">          <span class="built_in">console</span>.log(<span class="string">'停止'</span>)</span></span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="手动操控自定义组件"><a href="#手动操控自定义组件" class="headerlink" title="手动操控自定义组件"></a>手动操控自定义组件</h2><ul>
<li><p>场景一 :一些自定义组件，需要去获取组件对象进行一些其他的Dom操作</p>
</li>
<li><p>解决方法 :使用ref属性暴露组件获取句柄</p>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">​</span><br><span class="line">  <span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">el-progress</span> <span class="attr">type</span>=<span class="string">"circle"</span> <span class="attr">:percentage</span>=<span class="string">"O"</span> <span class="attr">ref</span>=<span class="string">"progress"</span>&gt;</span><span class="tag">&lt;/<span class="name">el-progress</span>&gt;</span><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">this</span>.$refs.progress <span class="comment">//组件对象实例， 可以手动调用组件的内置方法和属性</span></span></span><br><span class="line"><span class="actionscript">    <span class="keyword">this</span>.$refs.progress.$el <span class="comment">//组件 对象的最外层dom元素</span></span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">​</span><br></pre></td></tr></table></figure>
<h2 id="深度作用选择器"><a href="#深度作用选择器" class="headerlink" title="深度作用选择器"></a>深度作用选择器</h2><ul>
<li>场景一 : scoped的样式，希望影响到子组件的默认样式</li>
</ul>
<blockquote>
<p>在样式中设置完scoped在浏览器解析为如下图这样，a是个div，a div里面包含一个组件里面解析完了div的样式名字为b，想在父组件影响到子组件的默认样式。</p>
</blockquote>
<p>解决方法：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">​</span><br><span class="line">  <span class="tag">&lt;<span class="name">style</span> <span class="attr">scoped</span>&gt;</span></span><br><span class="line"><span class="css">    <span class="selector-class">.a</span> &gt;&gt;&gt; <span class="selector-class">.b</span> &#123; <span class="comment">/* ... */</span> &#125;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    //有些像Sass之类的预处理器无法正确解析&gt;&gt;&gt;。这种情况下你可以使用/deep/操作符取而代之- - - -这是一个&gt;&gt;&gt;的别名，同样可以正常工作。</span><br><span class="line">  <span class="tag">&lt;<span class="name">style</span> <span class="attr">scoped</span> <span class="attr">lang</span>=<span class="string">“scss”</span>&gt;</span></span><br><span class="line">    .a /deep/ .b &#123; /* ... */ &#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="Vue数组-对象更新视图不更新"><a href="#Vue数组-对象更新视图不更新" class="headerlink" title="Vue数组/对象更新视图不更新"></a>Vue数组/对象更新视图不更新</h2><ul>
<li>场景一 :很多时候我们习惯于这样操作数组和对象</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">​</span><br><span class="line">  data() &#123; </span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        arr: [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],</span><br><span class="line">        obj:&#123;</span><br><span class="line">          a: <span class="number">1</span>,</span><br><span class="line">          b: <span class="number">2</span> </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;; </span><br><span class="line">  &#125;,</span><br><span class="line">​</span><br><span class="line">  <span class="comment">// 数组更新视图不更新</span></span><br><span class="line">  <span class="keyword">this</span>.arr[<span class="number">0</span>] = <span class="string">'OBKoro1'</span>;</span><br><span class="line">  <span class="keyword">this</span>.arr.length = <span class="number">1</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(arr);<span class="comment">// ['OBKoro1']; </span></span><br><span class="line">  <span class="comment">// 数据更新，对象视图不更新    </span></span><br><span class="line">  <span class="keyword">this</span>.obj.c = <span class="string">'OBKoro1'</span>;</span><br><span class="line">  <span class="keyword">delete</span> <span class="keyword">this</span>.obj.a;</span><br><span class="line">  <span class="built_in">console</span>.log(obj);  <span class="comment">// &#123;b:2,c:'OBKoro1'&#125;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>解决方法 ：</p>
</blockquote>
<ul>
<li><p>this. $set(你要改变的数组/对象，你要改变的位置/key,你要改成什么value)</p>
</li>
<li><p>数组原生方法触发视图更新（ vue官网可查）:</p>
</li>
<li><p>整体替换数组/对象</p>
</li>
</ul>
<p><img src="https://user-gold-cdn.xitu.io/2018/10/19/1668a00148264e2c?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="vue"></p>
<h2 id="Vue-Filters过滤器的使用"><a href="#Vue-Filters过滤器的使用" class="headerlink" title="Vue Filters过滤器的使用"></a>Vue Filters过滤器的使用</h2><ul>
<li>场景一 :常见的数据文本的格式化</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// 在双花括号中</span></span><br><span class="line">  &lt;div&gt;&#123;&#123; message | DateFormat &#125;&#125;&lt;<span class="regexp">/div&gt; /</span><span class="regexp">/展示正确时间</span></span><br><span class="line"><span class="regexp">  /</span><span class="regexp">/ 在'v-bind'中</span></span><br><span class="line"><span class="regexp">  &lt;div v-bind:id="rawId | formatId"&gt;&lt;/</span>div&gt;</span><br><span class="line">​</span><br><span class="line"><span class="comment">//   Demo:一个日期过滤器返回yyyy- MM-ddhh:mm:ss的样式</span></span><br><span class="line"><span class="comment">//   引入一个提前写好日期格式化的js</span></span><br><span class="line">  <span class="keyword">import</span> dayjs <span class="keyword">from</span> <span class="string">'dayjs'</span>;</span><br><span class="line">  <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">     data() &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">                <span class="comment">//时间毫秒</span></span><br><span class="line">                message:<span class="number">18324798324789</span> </span><br><span class="line">            &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    filters: &#123;</span><br><span class="line">        <span class="comment">//传入进行日期格式化</span></span><br><span class="line">      DateFormat(value) &#123;</span><br><span class="line">        <span class="keyword">return</span> dayjs(value).format(“YYYY-MM-DD HH:mm:ss<span class="string">")</span></span><br><span class="line"><span class="string">        &#125; </span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">  &#125;</span></span><br></pre></td></tr></table></figure>
<h2 id="Vue深度watch与watch立即触发回调"><a href="#Vue深度watch与watch立即触发回调" class="headerlink" title="Vue深度watch与watch立即触发回调"></a>Vue深度watch与watch立即触发回调</h2><ul>
<li><p>场景一 :在watch里面监测对象里面对应的值是监测不到的，可以用如下方法。</p>
</li>
<li><p>选项: deep</p>
</li>
<li><p>在选项参数中指定deep:true,可以监听对象中子属性的变化。</p>
</li>
<li><p>选项: immediate</p>
</li>
<li><p>在选项参数中指定immediate:true,将立即以表达式的当前值触发回调，也就是默认触发一次。</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">watch: &#123;</span><br><span class="line">  obj: &#123;</span><br><span class="line">      handler(val, oldVal) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'属性变化触发这个回调'</span>,val, oldVal); </span><br><span class="line">      &#125;,</span><br><span class="line">      deep: <span class="literal">true</span> <span class="comment">// 监测这个对象中每一个属性的变化</span></span><br><span class="line">  &#125;,</span><br><span class="line">  step: &#123; <span class="comment">// 属性 //watch</span></span><br><span class="line">     handler(val, oldVal) &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">"默认触发一次"</span>, val, oldVal); </span><br><span class="line">     &#125;,</span><br><span class="line">     immediate: <span class="literal">true</span> <span class="comment">// 默认触发一次</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d='undefined'
    >Comments</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>张白告丶</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/zhanghao-web" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">149 <p>Articles</p></a></li>
                    <li><a href="/categories">23 <p>Categories</p></a></li>
                    <li><a href="/tags">47 <p>Tags</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>Contents</h4>
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#技巧-坑点"><span class="toc-number">1.</span> <span class="toc-text">技巧&#x2F;坑点</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#setTimeout-setInterval"><span class="toc-number">1.1.</span> <span class="toc-text">setTimeout&#x2F; setInterval</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#setInterval路由跳转继续运行并没有销毁"><span class="toc-number">1.1.1.</span> <span class="toc-text">setInterval路由跳转继续运行并没有销毁</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue路由拦截浏览器后退实现草稿保存类似需求"><span class="toc-number">1.2.</span> <span class="toc-text">Vue路由拦截浏览器后退实现草稿保存类似需求</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#自定义组件添加click等事件不生效"><span class="toc-number">1.3.</span> <span class="toc-text">自定义组件添加click等事件不生效</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#手动操控自定义组件"><span class="toc-number">1.4.</span> <span class="toc-text">手动操控自定义组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#深度作用选择器"><span class="toc-number">1.5.</span> <span class="toc-text">深度作用选择器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue数组-对象更新视图不更新"><span class="toc-number">1.6.</span> <span class="toc-text">Vue数组&#x2F;对象更新视图不更新</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-Filters过滤器的使用"><span class="toc-number">1.7.</span> <span class="toc-text">Vue Filters过滤器的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue深度watch与watch立即触发回调"><span class="toc-number">1.8.</span> <span class="toc-text">Vue深度watch与watch立即触发回调</span></a></li></ol></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            张白告丶
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
